﻿#include"XQHttpRequestObject.h"
#include"XQHttpClient.h"
#include"XQLog.hpp"
XQHttpRequestObject::XQHttpRequestObject(QObject* parent)
	:QObject(parent)
{
	//init();
}

XQHttpRequestObject::~XQHttpRequestObject()
{
}

QString XQHttpRequestObject::name() const
{
	return *m_pName;
}

XQHttpClient* XQHttpRequestObject::http() const
{
	return m_http;
}
bool XQHttpRequestObject::isConnected() const
{
	return m_http->isConnected();
}
//
//XQLog* XQHttpRequestObject::log() const
//{
//	return XQLog::Global(m_logName);
//}
//
//QString XQHttpRequestObject::logName() const
//{
//	return m_logName;
//}
//
//bool XQHttpRequestObject::isOutLog() const
//{
//	return m_outLog;
//}

void XQHttpRequestObject::setName(const QString& name)
{
	(*m_pName) = name;
}

void XQHttpRequestObject::bindName(QString* name)
{
	if (name)
		m_pName = name;
	else
		m_pName = &m_name;
}

void XQHttpRequestObject::setHttp(XQHttpClient* http)
{
	m_http = http;
	if (http)
		httpClient_init();
}

//void XQHttpRequestObject::setLogName(const QString& logName)
//{
//	auto log = this->log();
//	if (log)
//	{
//		log->setKey(logName);
//		m_logName = log->key();
//	}
//	else
//	{
//		XQLog::Create(logName, LogType::Debug, XQLog::cmd_out | XQLog::cmd_cpp | XQLog::cmd_queue | XQLog::showTime);
//		if(XQLog::Global(logName))
//			m_logName = logName;
//	}
//}
//
//void XQHttpRequestObject::setLogOut(bool out)
//{
//	m_outLog = out;
//}

void XQHttpRequestObject::connectToServer(const QString& url)
{
	m_http->connectToServer(url);
}

bool XQHttpRequestObject::connectToServerWait(const QString& url, int msecs)
{
	return m_http->connectToServerWait(url, msecs);
}

void XQHttpRequestObject::init()
{
	
}

void XQHttpRequestObject::httpClient_init()
{
	//m_http = new XQHttpClient(NetworkType::Tcp, this);
	connect(m_http, &XQHttpClient::connected, this, &XQHttpRequestObject::connected);
	connect(m_http, &XQHttpClient::disconnected, this, &XQHttpRequestObject::disconnected);
	connect(m_http, &XQHttpClient::headReplyRead, this, &XQHttpRequestObject::replyRead);
}

void XQHttpRequestObject::send(const QByteArray& data)
{
	m_http->post(data.toBase64());
}

//void XQHttpRequestObject::replyRead(const XQHttpHeadReply& head)
//{
//}
